package com.macro.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.IdWorker;
import com.macro.mall.dto.e.SysTalkEntDTO;
import com.macro.mall.mapper.BasicTopicMapper;
import com.macro.mall.model.BasicTopic;
import com.macro.mall.pojo.BasicTopicVo;
import com.macro.mall.query.BasicTopicQuery;
import com.macro.mall.service.SysTalkService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysTalkServiceImpl implements SysTalkService {

    @Autowired
    private BasicTopicMapper basicTopicMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public CommonResult list(SysTalkEntDTO sysTalkEntDTO) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(sysTalkEntDTO.getPage(), sysTalkEntDTO.getPageSize());
        BasicTopicQuery query = new BasicTopicQuery();
        BeanUtils.copyProperties(sysTalkEntDTO, query);
        List<BasicTopic> list = basicTopicMapper.list(query);
        List<BasicTopicVo> basicTopicVos = new ArrayList<>(list.size());
        for (BasicTopic basicTopic : list) {
            BasicTopicVo basicTopicVo = new BasicTopicVo();
            BeanUtils.copyProperties(basicTopic, basicTopicVo);
            if (basicTopic.getKeyList() != null && !basicTopic.getKeyList().equals("") && basicTopic.getKeyList().length() > 0) {
                List<String> parseArray = JSON.parseArray(basicTopic.getKeyList(), String.class);
                basicTopicVo.setKeyList(parseArray);
            }
            basicTopicVos.add(basicTopicVo);
        }
        PageInfo<BasicTopicVo> pageInfo = new PageInfo<>(basicTopicVos);
        resultMap.put("list", pageInfo.getList());
        resultMap.put("total", pageInfo.getTotal());
        return CommonResult.success(resultMap);
    }

    @Override
    public int modify(SysTalkEntDTO sysTalkEntDTO) {
        BasicTopic basicTopic = new BasicTopic();
        basicTopic.setId(String.valueOf(sysTalkEntDTO.getId()));
        basicTopic.setStatus(sysTalkEntDTO.getStatus());
        basicTopic.setTopicName(sysTalkEntDTO.getTalkName());
        return basicTopicMapper.updateByPrimaryKeySelective(basicTopic);
    }


    /***
     * 新增话题配置
     * @param topic
     * @return
     */
    @Override
    public CommonResult edit(BasicTopic topic) {
        if (topic.getId() == null || topic.getId().length() <= 0) {
            topic.setId(String.valueOf(idWorker.nextId()));
            topic.setCreatedDt(new Date());
            topic.setUpdatedDt(new Date());
            int row = basicTopicMapper.insertSelective(topic);
            return result(row, "新增失败");
        }
        topic.setUpdatedDt(new Date());
        int row = basicTopicMapper.updateByPrimaryKeySelective(topic);
        return result(row, "新增失败");
    }


    @Override
    public CommonResult delete(String id) {
        int row = basicTopicMapper.deleteByPrimaryKey(id);
        return result(row, "新增失败");
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }


}
